//
//  music_tables.h
//  CavernsSim
//
//  Created by Peter Barrett on 7/17/14.
//  Copyright (c) 2014 Peter Barrett. All rights reserved.
//

#ifndef CavernsSim_music_tables_h
#define CavernsSim_music_tables_h

/*
#include "math.h"
 static void dump(const char* s, int8_t* t)
 {
     printf("// %s\n",s);
     for (int i = 0; i < 256; i++) {
         printf("%4d,",*t++);
         if ((i & 0xF) == 0xF)
            printf("\n");
     }
 }

 static
 void make_audio_tables()
 {
    int8_t t[256];

    int i;
    for (i = 0; i < 256; i++)
        t[i] = sin(i*2*3.1415926535/256)*127;
    dump("Sine",t);

    for (i = 0; i < 256; i++)
        t[i] = i;
    dump("Sawtooth",t);

    for (i = 0; i < 256; i++)
    {
        t[i] = 127-(i << 1);
        if (i & 0x80)
            t[i] = -t[i];
    }
    dump("Triangle",t);

    for (i = 0; i < 256; i++)
        t[i] = i < 0x80 ? 127 : -127;
    dump("Square 50%",t);

     for (i = 0; i < 256; i++)
         t[i] = rand();
     dump("Random ",t);

    for (i = 0; i < 256; i++)
        t[i] = i < 0x40 ? 127 : -127;
    //dump("Square 25%",t);
 }
*/

PROGMEM
const int8_t _wave_tables[] = {
    //  Sine
    0,   3,   6,   9,  12,  15,  18,  21,  24,  27,  30,  33,  36,  39,  42,  45,
    48,  51,  54,  57,  59,  62,  65,  67,  70,  73,  75,  78,  80,  82,  85,  87,
    89,  91,  94,  96,  98, 100, 102, 103, 105, 107, 108, 110, 112, 113, 114, 116,
    117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125, 126, 126, 126, 126, 126,
    127, 126, 126, 126, 126, 126, 125, 125, 124, 123, 123, 122, 121, 120, 119, 118,
    117, 116, 114, 113, 112, 110, 108, 107, 105, 103, 102, 100,  98,  96,  94,  91,
    89,  87,  85,  82,  80,  78,  75,  73,  70,  67,  65,  62,  59,  57,  54,  51,
    48,  45,  42,  39,  36,  33,  30,  27,  24,  21,  18,  15,  12,   9,   6,   3,
    0,  -3,  -6,  -9, -12, -15, -18, -21, -24, -27, -30, -33, -36, -39, -42, -45,
    -48, -51, -54, -57, -59, -62, -65, -67, -70, -73, -75, -78, -80, -82, -85, -87,
    -89, -91, -94, -96, -98,-100,-102,-103,-105,-107,-108,-110,-112,-113,-114,-116,
    -117,-118,-119,-120,-121,-122,-123,-123,-124,-125,-125,-126,-126,-126,-126,-126,
    -127,-126,-126,-126,-126,-126,-125,-125,-124,-123,-123,-122,-121,-120,-119,-118,
    -117,-116,-114,-113,-112,-110,-108,-107,-105,-103,-102,-100, -98, -96, -94, -91,
    -89, -87, -85, -82, -80, -78, -75, -73, -70, -67, -65, -62, -59, -57, -54, -51,
    -48, -45, -42, -39, -36, -33, -30, -27, -24, -21, -18, -15, -12,  -9,  -6,  -3,
    
    //  Sawtooth
    0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
    16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
    32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
    48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
    64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
    80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
    96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
    112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
    -128,-127,-126,-125,-124,-123,-122,-121,-120,-119,-118,-117,-116,-115,-114,-113,
    -112,-111,-110,-109,-108,-107,-106,-105,-104,-103,-102,-101,-100, -99, -98, -97,
    -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81,
    -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65,
    -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49,
    -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33,
    -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17,
    -16, -15, -14, -13, -12, -11, -10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1,
    
    //  Triangle
    127, 125, 123, 121, 119, 117, 115, 113, 111, 109, 107, 105, 103, 101,  99,  97,
    95,  93,  91,  89,  87,  85,  83,  81,  79,  77,  75,  73,  71,  69,  67,  65,
    63,  61,  59,  57,  55,  53,  51,  49,  47,  45,  43,  41,  39,  37,  35,  33,
    31,  29,  27,  25,  23,  21,  19,  17,  15,  13,  11,   9,   7,   5,   3,   1,
    -1,  -3,  -5,  -7,  -9, -11, -13, -15, -17, -19, -21, -23, -25, -27, -29, -31,
    -33, -35, -37, -39, -41, -43, -45, -47, -49, -51, -53, -55, -57, -59, -61, -63,
    -65, -67, -69, -71, -73, -75, -77, -79, -81, -83, -85, -87, -89, -91, -93, -95,
    -97, -99,-101,-103,-105,-107,-109,-111,-113,-115,-117,-119,-121,-123,-125,-127,
    -127,-125,-123,-121,-119,-117,-115,-113,-111,-109,-107,-105,-103,-101, -99, -97,
    -95, -93, -91, -89, -87, -85, -83, -81, -79, -77, -75, -73, -71, -69, -67, -65,
    -63, -61, -59, -57, -55, -53, -51, -49, -47, -45, -43, -41, -39, -37, -35, -33,
    -31, -29, -27, -25, -23, -21, -19, -17, -15, -13, -11,  -9,  -7,  -5,  -3,  -1,
    1,   3,   5,   7,   9,  11,  13,  15,  17,  19,  21,  23,  25,  27,  29,  31,
    33,  35,  37,  39,  41,  43,  45,  47,  49,  51,  53,  55,  57,  59,  61,  63,
    65,  67,  69,  71,  73,  75,  77,  79,  81,  83,  85,  87,  89,  91,  93,  95,
    97,  99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127,
    
    //  Square 50%
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    -127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,
    
    // Random
    -89, -15, -39,  42,-126, -56, -40,  -2,  67,  77,-104,  85,-116, -30, -77,  71,
    23,  17,-104,  84,  47,  17,  45,   5,  88, -11, 107, -42,-120,   7,-103,-110,
    72,  51,  98,  65, -13,  13,  35, -27,  95,  48, -47, -56, -19,  97,  12,  75,
    2,  53,  57,-127,-124, -72,  20, -94,-100, -76,  90, 103,  42, -54, -27,  72,
    -23, -59, -15, -80, -60,  21,-118, -27,-101,  77,  57, -10,  -9, -24, -95,   5,
    -45,  -2, -19, -91, -43, -13, -39, -28,  91,  -6, 108, -61,  81, -30,  32, -82,
    12, -31,   6,-104, 109,  97,  -1,  52, -95,  30,  25,  -3,  54,  80, -23, -73,
    -127,-113, -61,  58,  30,  15, -64,  44,  68,  85, 122, -56, -85,  80, -55, -78,
    -34, -78, -10, -75, -30,  76,  79, -35, -97,-120, 103, -67, -50,  31, -14,  97,
    0,-114, 120,-105,-105,  14,  52,  98,   7, -41,  94,  71, -95,  88,  41,-114,
    91, -94, -11,  98,  70,-122,-100, -60,  46,  54,  42,   2, 115,  18, 100, -26,
    6,-121, -17,  83,   9, -47,   8,  83,  79,  81,  -8, 101,-113, -76, -16,-128,
    -73, -53,  25, -18,-102, -21, -41,  24, -52,  79, -94, 124,-116,  55, -33, -63,
    -83, -91, -47,  51, -47,  58, -66,   3, -16,  33, -23, -79, -73,-116, -53, -40,
    47, 127, -14, -77,-116, 109,  72, -48,  30,  72,  27,  45,  79, -81, 113, 113,
    -128,  95, -41, -14, -45, -98, -12, -60, -15,-101,-108,-106, -24,  29, -85,-127
};

/*
 // audio sample rate is 15750000/1001 hz
 #include "math.h"
 void make_midi_table()
 {
     float a = 440;
     float rate = 15750000/1001;
     for (float x = 0; x < 128; ++x)
     {
        float f = (a / 32) * pow(2,(x - 9) / 12);
        printf("0x%04X, // %3d %fhz\n",(int)(0x10000*f/rate),(int)x,f);
     }
 }
 */

// 0..127 midi notes converted to audio engine steps
const uint16_t _midi_tables[] PROGMEM = {
    0x0022, //   0 8.175799hz
    0x0024, //   1 8.661957hz
    0x0026, //   2 9.177024hz
    0x0028, //   3 9.722718hz
    0x002A, //   4 10.300861hz
    0x002D, //   5 10.913383hz
    0x0030, //   6 11.562325hz
    0x0033, //   7 12.249857hz
    0x0036, //   8 12.978271hz
    0x0039, //   9 13.750000hz
    0x003C, //  10 14.567617hz
    0x0040, //  11 15.433853hz
    0x0044, //  12 16.351599hz
    0x0048, //  13 17.323915hz
    0x004C, //  14 18.354048hz
    0x0050, //  15 19.445436hz
    0x0055, //  16 20.601723hz
    0x005A, //  17 21.826765hz
    0x0060, //  18 23.124651hz
    0x0066, //  19 24.499714hz
    0x006C, //  20 25.956545hz
    0x0072, //  21 27.500000hz
    0x0079, //  22 29.135237hz
    0x0080, //  23 30.867706hz
    0x0088, //  24 32.703197hz
    0x0090, //  25 34.647831hz
    0x0098, //  26 36.708096hz
    0x00A1, //  27 38.890873hz
    0x00AB, //  28 41.203445hz
    0x00B5, //  29 43.653526hz
    0x00C0, //  30 46.249302hz
    0x00CC, //  31 48.999432hz
    0x00D8, //  32 51.913086hz
    0x00E5, //  33 55.000000hz
    0x00F2, //  34 58.270466hz
    0x0101, //  35 61.735416hz
    0x0110, //  36 65.406395hz
    0x0120, //  37 69.295654hz
    0x0131, //  38 73.416199hz
    0x0143, //  39 77.781746hz
    0x0157, //  40 82.406883hz
    0x016B, //  41 87.307060hz
    0x0181, //  42 92.498604hz
    0x0198, //  43 97.998856hz
    0x01B0, //  44 103.826180hz
    0x01CA, //  45 110.000000hz
    0x01E5, //  46 116.540932hz
    0x0202, //  47 123.470833hz
    0x0220, //  48 130.812790hz
    0x0241, //  49 138.591309hz
    0x0263, //  50 146.832397hz
    0x0287, //  51 155.563492hz
    0x02AE, //  52 164.813766hz
    0x02D7, //  53 174.614120hz
    0x0302, //  54 184.997208hz
    0x0330, //  55 195.997711hz
    0x0360, //  56 207.652359hz
    0x0394, //  57 220.000000hz
    0x03CA, //  58 233.081909hz
    0x0404, //  59 246.941620hz
    0x0441, //  60 261.625580hz
    0x0482, //  61 277.182648hz
    0x04C7, //  62 293.664734hz
    0x050F, //  63 311.126984hz
    0x055C, //  64 329.627594hz
    0x05AE, //  65 349.228180hz
    0x0605, //  66 369.994415hz
    0x0660, //  67 391.995483hz
    0x06C1, //  68 415.304657hz
    0x0728, //  69 440.000000hz
    0x0795, //  70 466.163818hz
    0x0809, //  71 493.883240hz
    0x0883, //  72 523.251160hz
    0x0905, //  73 554.365295hz
    0x098E, //  74 587.329468hz
    0x0A1F, //  75 622.253967hz
    0x0AB9, //  76 659.255188hz
    0x0B5D, //  77 698.456360hz
    0x0C0A, //  78 739.988831hz
    0x0CC1, //  79 783.990967hz
    0x0D83, //  80 830.609314hz
    0x0E51, //  81 880.000000hz
    0x0F2B, //  82 932.327637hz
    0x1012, //  83 987.766479hz
    0x1106, //  84 1046.502319hz
    0x120A, //  85 1108.730591hz
    0x131C, //  86 1174.658936hz
    0x143F, //  87 1244.507935hz
    0x1573, //  88 1318.510376hz
    0x16BA, //  89 1396.912720hz
    0x1814, //  90 1479.977661hz
    0x1983, //  91 1567.981934hz
    0x1B07, //  92 1661.218628hz
    0x1CA2, //  93 1760.000000hz
    0x1E56, //  94 1864.655273hz
    0x2024, //  95 1975.532959hz
    0x220D, //  96 2093.004639hz
    0x2414, //  97 2217.461182hz
    0x2639, //  98 2349.317871hz
    0x287F, //  99 2489.015869hz
    0x2AE7, // 100 2637.020752hz
    0x2D74, // 101 2793.825439hz
    0x3028, // 102 2959.955322hz
    0x3306, // 103 3135.963867hz
    0x360E, // 104 3322.437256hz
    0x3945, // 105 3520.000000hz
    0x3CAD, // 106 3729.309326hz
    0x4049, // 107 3951.067383hz
    0x441B, // 108 4186.009277hz
    0x4828, // 109 4434.920898hz
    0x4C72, // 110 4698.637207hz
    0x50FE, // 111 4978.031738hz
    0x55CF, // 112 5274.039551hz
    0x5AE9, // 113 5587.652832hz
    0x6051, // 114 5919.910645hz
    0x660C, // 115 6271.925781hz
    0x6C1D, // 116 6644.876465hz
    0x728B, // 117 7040.000000hz
    0x795A, // 118 7458.618652hz
    0x8092, // 119 7902.134766hz
    0x8837, // 120 8372.018555hz
    0x9051, // 121 8869.841797hz
    0x98E5, // 122 9397.274414hz
    0xA1FD, // 123 9956.063477hz
    0xAB9F, // 124 10548.079102hz
    0xB5D3, // 125 11175.305664hz
    0xC0A3, // 126 11839.821289hz
    0xCC18, // 127 12543.851562hz
};

#endif
